package tgc.edu.yzy.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.TreeDTO;
import tgc.edu.yzy.entity.Tree;
import tgc.edu.yzy.service.TreeService;

@Controller
@RequestMapping(value="tree")
public class TreeController {
	@Autowired
	private TreeService treeservice;
	@RequestMapping(value="treelist")
	@ResponseBody
	private Object treelist(Integer id) {
		List<TreeDTO> trees=treeservice.tree(id);
		return trees;
	}
	//可推拽
	@RequestMapping(value="changeLevel")
	@ResponseBody
	private String changeLevel(Integer targetId,Integer sourceId,String point) {
		//得到目标对象
		Tree treetargent=treeservice.findById(targetId);
		//得到操作的对象
		Tree treesource=treeservice.findById(sourceId);
		//append top bottom
		if("append".equals(point)) {
			treesource.setParent(treetargent);
		}else {
			treesource.setParent(treetargent.getParent());
		} 
		treeservice.save(treesource);
		return "ok";
	}
	@RequestMapping(value="treeadd")
	@ResponseBody
	private String treeadd(Integer parentId	,Tree treeform) {
		if(treeform.getId()==null||treeform.getId()==0) {
			Tree trees=new Tree();
			trees.setParent(treeservice.findById(parentId));
			trees.setName(treeform.getName());
			trees.setUrl(treeform.getUrl());
			treeservice.save(trees);
		}else {
			Tree trees=treeservice.findById(treeform.getId());
			trees.setName(treeform.getName());
			trees.setUrl(treeform.getUrl());
			treeservice.save(trees);
		}
		return "";
	}
	@RequestMapping(value="remove")
	@ResponseBody
	private String remove(Integer id) {
		treeservice.deleteByID(id);
		//在不借助框架时，如何删除 递归删除节点
		//treeservice.treedelete(id);
		return "ok";
	}
}
